using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Linq.Dynamic;
using YTS.Tools;

namespace Test.ConsoleProgram.Algorithm.Solution
{
    [TestDescription("算法: 0050. 计算 x 的 n 次幂函数")]
    public class No0050_Pow : AbsBaseTestItem
    {
        public override void OnTest()
        {
            Assert.TestExe(MyPow, 2.00000, 10, 1024.000);
            Assert.TestExe(MyPow, 2.10000, 3, 9.26100);
            Assert.TestExe(MyPow, 2.00000, -2, 0.25000);
            Assert.TestExe(MyPow, 0.44528, 0, 1.00);
            Assert.TestExe(MyPow, 0.00001, 2147483647, 0);
        }

        public double quickMul(double x, long N)
        {
            if (N == 0)
            {
                return 1.0;
            }
            double y = quickMul(x, N / 2);
            double value = 0.0;
            if (N % 2 == 0)
            {
                value = y * y;
            }
            else
            {
                value = y * y * x;
            }
            return value;
        }

        public double MyPow(double x, int n)
        {
            long N = n;
            double value = 0.0;
            if (N >= 0)
            {
                value = quickMul(x, N);
            }
            else
            {
                value = 1.0 / quickMul(x, -N);
            }
            value = ConvertTool.ToFixed(value, 3);
            return value;
        }
    }
}
